<template>
  <div>
   
  </div>
</template>
<script>
import axios from 'axios'
import { mapState } from 'vuex'
import {Get} from '@/config/api.js'

export default {
  name: 'header',
  data() {
    return {
      title: '',
      navData:'',
      search:'',//搜索
      searchStart:false,//搜索框下拉状态
      searchList:[],//搜索记录
      cartStart:false,
      focusStatus:false,
      cartList:'',//购物车列表
      navStart:false,
      id:1
    }
  },
  mounted(){
    this.getNav()
  },
  watch:{
    search(){
      if(this.search.length>0){
        this.searchStart=false
      }
    }
  },
  computed: {
    ...mapState({
      login: state => state.login,
      headerH: state => state.headerH,
      headerShow: state => state.headerShow,
      cartShow: state => state.cartStart
    })
  },
  methods: {
    //鼠标移上去添加样式
    navIn(){
      this.navStart=true
    },
    //鼠标移上去添加样式
    navOut(){
      this.navStart=false
    },
    async getNav(){

        if(this.vCheck()){
          let reg=await Get('/goods/category/all?_sort=category_sort',this.$store.state.token)
          this.navData=reg.data
          
          let that=this
          this.navData.forEach(function (value,i) {
              value.son.forEach(function(val,j){
                that.$set(val,'start',false);
              })
              that.$set(value,'start',false);
          });
        }
    },
    //点击查找
    keepLook(){
      if(this.search!=''){
        let look;
        let token;
        let that=this;
        let name = "look" + "=";
        let ca = document.cookie.split(';');
        for (let i = 0; i < ca.length; i++) {
            let c = ca[i].split("=");
            if(this.trimStr(c[0])=="look"){
                look=c[1];
                let arr=look.split(',')
                that.searchList=arr.reverse()
            }
        };
        if(look!=undefined){
          token=look+','+this.search
        }else{
          token=this.search
        }
        var d = new Date();
        d.setTime(d.getTime() + (604800000));
        var expires = "expires=" + d.toUTCString();
        document.cookie = "look" + "=" + token + "; " + expires;
        this.$router.push({path:'/prcSearch',query: {id:this.search}})
        this.search=''
      }
    },
    //顶级栏目
    getSon(val){
      let arr=[];
      for(let i=0;i<val.length;i++){
        arr.push(val[i].id)
      }
      return arr
    },
    //搜索框下拉显示
    searchShow(){
        if(this.search.length<1){
          let look;
          let that=this;
          let name = "look" + "=";
          let ca = document.cookie.split(';');
          for (let i = 0; i < ca.length; i++) {
              let c = ca[i].split("=");
              if(this.trimStr(c[0])=="look"){
                  look=c[1];
                  let arr=look.split(',')
                  that.searchList=arr.reverse()
              }
          };
          this.searchStart=true
        }
    },
    //搜索框下拉隐藏
    searchHide(){
        this.searchStart=false
    },
    //搜索框下拉选择
    searchLi(val){
        this.searchStart=false
        this.search=val
        document.getElementById('input').focus()
    },
    searchEnt(){
      if(this.search!=''){
        let look;
        let token;
        let that=this;
        let name = "look" + "=";
        let ca = document.cookie.split(';');
        for (let i = 0; i < ca.length; i++) {
            let c = ca[i].split("=");
            if(this.trimStr(c[0])=="look"){
                look=c[1];
                let arr=look.split(',')
                that.searchList=arr.reverse()
            }
        };
        if(look!=undefined){
          token=look+','+this.search
        }else{
          token=this.search
        }
        var d = new Date();
        d.setTime(d.getTime() + (604800000));
        var expires = "expires=" + d.toUTCString();
        document.cookie = "look" + "=" + token + "; " + expires;
        this.$router.push({path:'/prcSearch',query: {id:this.search}})
        this.search=''
      }
    },
    //导航淡化效果
    itemIn(e){
      let that=this
      this.navData.forEach(function (value,i) {
          that.$set(value,'start',true);
      });
      e.start=false

    },
    itemOut(){
      let that=this
      this.navData.forEach(function (value,i) {
          that.$set(value,'start',false);
      });
    },
    listIn(e){
      let that=this
      this.navData.forEach(function (value,i) {
          value.son.forEach(function(val,j){
            that.$set(val,'start',true);
          })
      });
      e.start=false

    },
    listOut(){
      let that=this
      this.navData.forEach(function (value,i) {
          value.son.forEach(function(val,j){
            that.$set(val,'start',false);
          })
      });
    },
    //点击登录
    loginBox(){
      this.$store.dispatch("loginBox");
    },
    //退出登录
    closeLogin(){
      document.cookie = "login="+"; " +"-1";
      this.$store.dispatch("closelogin");
      this.$router.push({ path: '/'})

    },
    //弹出购物车
    async cartTop(){
      
      if(this.vLogin()){
        this.$store.dispatch("cartOpen")
        let that=this
        document.body.setAttribute("class","bodyCart");
        await Get('/carts?_sort=id&_pagination=false',this.$store.state.token).then(function(res){
          that.cartList=res.data
        }).catch(function(err){
          alert('获取购物车失败！')
        })
        
        
      }
    },
    //更新购物车信息
    async getNow(){
        let reg=await Get('/carts?_sort=id',this.$store.state.token)
        this.cartList=reg.data
    },
    closeCartTop(){
      document.body.setAttribute("class","");
      this.$store.dispatch("cartClose")
    }
  }
}

</script>
